package bold.deposit.useful;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class CookieUtil {
    private static final int MAX_TIME = (int) TimeUnit.DAYS.toSeconds(7);

    public static void delete(HttpServletRequest request, HttpServletResponse response, String name) {
        if (null == name) {
            return;
        }
        Cookie cookie = getCookie(request, name);
        if (null != cookie) {
            cookie.setPath("/");
            cookie.setValue("");
            cookie.setMaxAge(0);
            response.addCookie(cookie);
        }
    }

    public static String get(HttpServletRequest request, String name) {
        Cookie cookie = getCookie(request, name);
        if (cookie == null) {
            return null;
        } else {
            String v = null;
            try {
                v = java.net.URLDecoder.decode(cookie.getValue(), "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return v;
        }
    }

    /**
     * 添加一条新的Cookie，默认7天过期时间(单位：秒)
     */
    public static void set(HttpServletResponse response, String name, String value) {
        set(response, name, value, MAX_TIME);
    }

    /**
     * maxValue 单位秒
     */
    public static void set(HttpServletResponse response, String name, String value, int maxValue) {
        if (null == name) {
            return;
        }
        if (null == value) {
            value = "";
        }
        Cookie cookie = null;
        try {
            cookie = new Cookie(name, URLEncoder.encode(value, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        cookie.setPath("/");
        if (maxValue != 0) {
            cookie.setMaxAge(maxValue);
        } else {
            cookie.setMaxAge(MAX_TIME);
        }
        response.addCookie(cookie);
    }

    private static Cookie getCookie(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        if (null == cookies || null == name || name.length() == 0) {
            return null;
        }
        Cookie cookie = null;
        for (Cookie c : cookies) {
            if (name.equals(c.getName())) {
                cookie = c;
                break;
            }
        }
        return cookie;
    }

    public static Map<String, String> getCookieAll(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        Map<String, String> map = new LinkedHashMap<>();
        if (null != cookies) {
            for (Cookie cookie : cookies) {
                try {
                    map.put(cookie.getName(), java.net.URLDecoder.decode(cookie.getValue(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }
        }
        return map;
    }
}
